package cc.idoone.spring_boot_hello.amqp.object;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 发送对象(需实现接口 Serializable)
 *
 * @author hemj
 * @since 2017/10/15
 */
@Configuration
public class RabbitConfig {

	static final String DIRECT_KEY = "spring-boot-object";
	static final String QUENE = "spring-boot-object";
	static final String EXCHANGE = "spring-boot-exchange1";

	@Bean(name = "queueObject")
	Queue queue() {
		// queue 的 durable 属性的值定义后不可以修改，若修改则 queue 需要另起名称
		return new Queue(QUENE, true);
	}

	@Bean(name = "bindingObject")
	Binding binding(@Qualifier("queueObject") Queue queue, @Qualifier("exchange") TopicExchange exchange) {
		return BindingBuilder.bind(queue).to(exchange).with(DIRECT_KEY);
	}

	@RabbitListener(queues = "spring-boot-object")
	public void receiveQueue(UserObject message) {
		System.out.println("Object: " + message);
	}

}
